package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.TreeSet;

import javax.swing.JTable;

import model.Absence;
import model.College;
import model.Teacher;
import dataLayer.DAOAbsence;
import dataLayer.DBconnect;
import types.ConsoleType;
import view.PanelDASboard;
import view.PanelEditAbsence;
import view.PanelHolder;

public class EditAbsence extends DBconnect implements ActionListener {

	private PanelEditAbsence PEAform;
	private int absence_Id;
	private String absence_Teacher;
	private String absence_Substitude;
	private int absence_Hours;
	private String absence_Note;
	private java.util.Date absence_StartDate;
	private java.util.Date absence_EndDate;
	private String absence_Reason;
	private String test;
	private TreeSet<Absence> absenceList = new TreeSet<Absence>();
	private boolean set = true;
	String message = null;
	ConsoleType type = null;

	public EditAbsence(PanelEditAbsence PEAform) {
		this.setPEAform(PEAform);
		setAbsenceList(DAOAbsence.getInstance().loadAllAbsence());
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		openConn();
		if (e.getActionCommand().equals("Opslaan")) {

			Absence A = new Absence(absence_Id, absence_Teacher,
					absence_Substitude, absence_Hours, absence_Note,
					absence_StartDate, absence_EndDate, absence_Reason);

			A = PEAform.getAbsence();

			JTable absenceTableToday = PanelDASboard.getInstance()
					.getAbsenceListToday();
			JTable absenceTableWeek = PanelDASboard.getInstance()
					.getAbsenceListWeek();
			try {
				// checks which table is selected
				if (PanelDASboard.getInstance().getSelectedTable() == absenceTableToday) {
					// if last selection is on today table
					absence_Id = (int) absenceTableToday.getModel().getValueAt(
							absenceTableToday.getSelectedRow(), 5);

					absence_Teacher = (String) absenceTableToday.getModel()
							.getValueAt(absenceTableToday.getSelectedRow(), 0);
					set = true;
				}
				if (PanelDASboard.getInstance().getSelectedTable() == absenceTableWeek) {
					// if last selection is on week table
					absence_Id = (int) absenceTableWeek.getModel().getValueAt(
							absenceTableWeek.getSelectedRow(), 5);

					absence_Teacher = (String) absenceTableWeek.getModel()
							.getValueAt(absenceTableWeek.getSelectedRow(), 0);
					set = false;
				}
				if (absence_Id == 0){
					
					message = "Selecteer een absentie";
					type = ConsoleType.ERROR;
					Console.getInstance().setConsole(message, type);
				}
				else {
					

				Absence A1 = new Absence(A.getAbsence_Id(), absence_Teacher,
						absence_Substitude, absence_Hours, absence_Note,
						absence_StartDate, absence_EndDate, absence_Reason);

				absence_Teacher = PEAform.getUitvallerCombo().getSelectedItem()
						.toString();
				absence_Substitude = PEAform.getInvallerCombo()
						.getSelectedItem().toString();
				test = PEAform.getJThours().getText();

				absence_Hours = Integer.parseInt(test);
				absence_StartDate = (java.util.Date) PEAform.getJSStartDate()
						.getValue();
				absence_EndDate = (java.util.Date) PEAform.getJSEndDate()
						.getValue();
				absence_Reason = PEAform.getRedenCombo().getSelectedItem()
						.toString();
				absence_Note = PEAform.getJTNote().getText();

				A1.setAbsence_EndDate(absence_EndDate);
				A1.setAbsence_Hours(absence_Hours);
				A1.setAbsence_Note(absence_Note);
				A1.setAbsence_Reason(absence_Reason);
				A1.setAbsence_StartDate(absence_StartDate);
				A1.setAbsence_Substitude(absence_Substitude);
				A1.setAbsence_Teacher(absence_Teacher);

				// checks for empty fields
				if (absence_EndDate.equals("") && absence_EndDate.equals((""))) {

				} else if (absence_Reason.equals("")
						&& absence_Substitude.equals("")
						&& absence_Teacher.equals("")) {

				} else if (PEAform.getJThours().equals("")) {
				} else if (set == true) {
					// updates the absence
					Teacher.getInstance().getAllAbsencesToday().remove(A);
					DAOAbsence.getInstance().updateAbsence(A1);
					Teacher.getInstance().getAllAbsencesToday().add(A1);

					Teacher.getInstance().getAllAbsencesWeek().remove(A);
					Teacher.getInstance().getAllAbsencesWeek().add(A1);

					College.getInstance().setAbsenceListToday();
				} else if (set == false) {
					Date today = new Date();
					System.out.println(today.getDate());
					Date max = A1.getAbsence_EndDate();
					Date min = A1.getAbsence_StartDate();
					if (today.after(min) && (today.before(max))) {
						Teacher.getInstance().getAllAbsencesToday().remove(A);
						Teacher.getInstance().getAllAbsencesToday().add(A1);
					}

					Teacher.getInstance().getAllAbsencesWeek().remove(A);
					DAOAbsence.getInstance().updateAbsence(A1);
					Teacher.getInstance().getAllAbsencesWeek().add(A1);
					System.out.println("week editabsence");
					College.getInstance().setAbsenceListWeek();

				}
				PanelDASboard.getInstance().repaint();
				PanelDASboard.getInstance().validate();

				PanelHolder Ph = PanelHolder.getInstance();
				PanelDASboard PDB = PanelDASboard.getInstance();
				Ph.remove(PEAform);
				Ph.add(PDB);
				Ph.repaint();
				Ph.validate();
				}
			} catch (ArrayIndexOutOfBoundsException e1) {
				message = "Selecteer eerst een absentie";
				type = ConsoleType.ERROR;
				Console.getInstance().setConsole(message, type);
			}
		}
	}

	public PanelEditAbsence getPEAform() {
		return PEAform;
	}

	public void setPEAform(PanelEditAbsence pEAform) {
		PEAform = pEAform;
	}

	public TreeSet<Absence> getAbsenceList() {
		return absenceList;
	}

	public void setAbsenceList(TreeSet<Absence> absenceList) {
		this.absenceList = absenceList;
	}
}
